找传奇、传世资源到传世资源站!

Wpf 调用外部字库生成透明阴影图像

8.5玩家评分(1人评分)
下载后可评
介绍 评论 失效链接反馈

调用外部字库、文字阴影生成图像,“百度”到处都有。
“百度”一般会告诉你一个错误的,但离正确不远的结果。
1、调用外部字库,搜索结果是放入资源文件中,一个字库7、8兆,放上几个,那exe得多大,
在一些博客上找到了方法,是错的,已在评论中纠正。
2、文字阴影可以转为图像,但组件须已显示,比如:
一副图像加上水印文字,DrawText可以直接画,但画模糊阴影文字无法画上去。

本例根据图像大小,画文字自动换行显示,原理是将文字生成背景透明的阴影图像,再画到图像上去,生成1幅图像。如图像Dpi不为96,调整文字大小或自行修改代码适应。

/// <summary> /// 组件生成图片 /// </summary> /// <param name="control">组件名称</param> /// <param name="filename">保存路径</param> private void SaveControlImage(FrameworkElement control, string filename) { Rect rect = VisualTreeHelper.GetDescendantBounds(control); DrawingVisual dv = new DrawingVisual(); using (DrawingContext ctx = dv.RenderOpen()) { VisualBrush brush = new VisualBrush(control); ctx.DrawRectangle(brush, null, new Rect(rect.Size)); } int width = (int)control.ActualWidth; int height = (int)control.ActualHeight; RenderTargetBitmap rtb = new RenderTargetBitmap(width, height, 96, 96, PixelFormats.Pbgra32); rtb.Render(dv); PngBitmapEncoder encoder = new PngBitmapEncoder(); encoder.Frames.Add(BitmapFrame.Create(rtb)); using (FileStream fs = new FileStream(filename,FileMode.Create, FileAccess.Write, FileShare.None)) { encoder.Save(fs); } }/// <summary> /// 生成背景透明阴影文字图像 /// </summary> /// <param name="content">文字内容</param> /// <param name="fontFace">字体</param> /// <param name="fontSize">字体大小</param> /// <param name="fontColor">文字颜色</param> /// <param name="textWidth">文字范围宽度</param> /// <param name="textHeight">文字范围高度</param> /// <param name="_dpiX">点数</param> /// <param name="_dpiY">点数</param> /// <param name="shodowOffset">阴影距离</param> /// <param name="_radius">模糊值</param> /// <param name="_opacity">阴影透明度</param> /// <param name="beginColor">文字渐变色起始颜色</param> /// <param name="endColor">文字渐变色结束颜色</param> /// <returns></returns> public static RenderTargetBitmap DrawShadowText( string content, string fontFace, double fontSize, Brush fontColor, int textWidth, int textHeight, int _dpiX, int _dpiY, Point shodowOffset, int _radius, double _opacity, Color beginColor, Color endColor ) { var visualShadow = new DrawingVisual(); using (DrawingContext drawingShadowContext = visualShadow.RenderOpen()) { var pixels = new byte[textWidth * textHeight * 4]; var maxWH = textWidth > textHeight ? textWidth : textHeight; BitmapSource bitmapSource = BitmapSource.Create(textWidth, textHeight, _dpiX, _dpiY, PixelFormats.Pbgra32, null, pixels, maxWH * 4); drawingShadowContext.DrawImage(bitmapSource, new Rect(0, 0, textWidth, textHeight)); FormattedText formatshadow = new FormattedText(content, CultureInfo.InvariantCulture, FlowDirection.LeftToRight, new Typeface(fontFace), fontSize, Brushes.Black); formatshadow.MaxTextWidth = textWidth; formatshadow.MaxTextHeight = textHeight; formatshadow.SetFontWeight(FontWeights.Normal, 0, content.Length); drawingShadowContext.DrawText(formatshadow, shodowOffset); } var image = new DrawingImage(visualShadow.Drawing); Rectangle rct = new Rectangle() { Fill = new ImageBrush(image), Effect = new BlurEffect() { Radius = _radius }, Opacity = _opacity, }; Size sz = new Size(image.Width, image.Height); rct.Measure(sz); rct.Arrange(new Rect(sz)); RenderTargetBitmap rtbmp = new RenderTargetBitmap(textWidth, textHeight, _dpiX, _dpiY, PixelFormats.Default); rtbmp.Render(rct); BitmapSource bgImage = rtbmp; RenderTargetBitmap composeImage = new RenderTargetBitmap(bgImage.PixelWidth, bgImage.PixelHeight, _dpiX, _dpiY, PixelFormats.Default); DrawingVisual drawingVisual = new DrawingVisual(); FormattedText formattext = new FormattedText(content, CultureInfo.InvariantCulture, FlowDirection.LeftToRight, new Typeface(fontFace), fontSize, fontColor); formattext.MaxTextWidth = textWidth; formattext.MaxTextHeight = textHeight; formattext.SetFontWeight(FontWeights.Normal, 0, content.Length); formattext.SetForegroundBrush( new LinearGradientBrush( beginColor, endColor, fontSize), 0, content.Length); using (DrawingContext drawingContext = drawingVisual.RenderOpen()) { drawingContext.DrawImage(bgImage, new Rect(0, 0, bgImage.Width, bgImage.Height)); drawingContext.DrawText(formattext, new Point(0, 0)); } composeImage.Render(drawingVisual); return composeImage; }

Wpf 调用外部字库生成透明阴影图像 C#图形和图像处理-第1张

评论

发表评论必须先登陆, 您可以 登陆 或者 注册新账号 !


在线咨询: 问题反馈
客服QQ:174666394

有问题请留言,看到后及时答复